package com.android.yinghebike.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;

import com.android.baselibrary.interface_.OkHttpCallBack;
import com.android.baselibrary.responce.BaseResponce;
import com.android.baselibrary.tool.CommToast;
import com.android.baselibrary.tool.SPUtil;
import com.android.baselibrary.view.Comm_EditView;
import com.android.baselibrary.view.Comm_HeadView;
import com.android.baselibrary.view.MultiStateView;
import com.android.yinghebike.R;
import com.android.yinghebike.bean.UserInfo;
import com.android.yinghebike.constants.Constants;
import com.android.yinghebike.constants.SPConstants;
import com.android.yinghebike.manager.API_Manager;
import com.android.yinghebike.manager.UserManager;

import org.litepal.LitePal;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity1 extends BaseActivity {
    @BindView(R.id.comm_title)
    Comm_HeadView comm_title;
    @BindView(R.id.et_account)
    Comm_EditView et_account;
    @BindView(R.id.et_pwd)
    Comm_EditView et_pwd;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login1;
    }

    @Override
    protected void initView() {
        comm_title.setTitle(getString(R.string.login_title));
        comm_title.setLeftImgVisible(View.GONE);
    }

    @Override
    protected void initData() {
        String account = SPUtil.getStringValue(mContext, SPConstants.Phone, "");
        if (!TextUtils.isEmpty(account)) {
            et_account.setText(account);
        }
    }

    @OnClick({R.id.tv_regist, R.id.tv_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_regist:
                StartActivity(RegistActivity.class);
                break;
            case R.id.tv_login:
                login();
                break;
        }
    }

    private void login() {
        String account = et_account.getText();
        String pwd = et_pwd.getText();
        if (TextUtils.isEmpty(account)) {
            CommToast.showToast(mContext, getString(R.string.login_hint_account));
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            CommToast.showToast(mContext, getString(R.string.login_hint_pwd));
            return;
        }
        List<UserInfo> userInfos = LitePal.where("account=?", account).find(UserInfo.class);
        if (userInfos != null && userInfos.size() > 0) {
            UserInfo userInfo = userInfos.get(0);
            if (userInfo.pwd.equals(pwd)) {
                //登录成功
                UserManager.saveUserInfo(mContext, userInfo);
                SPUtil.putValue(mContext, SPConstants.Phone, account);
                UserManager.toMainActivty(mContext);
                finish();
            } else {
                CommToast.showToast(mContext, getString(R.string.login_tips_pwd_error));
            }
        } else {
            CommToast.showToast(mContext, getString(R.string.login_tips_unregist));
        }
    }
}
